package com.rzy.k_cas.b;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

// 该例子演示了AtomicInteger的方法天生就具备原子性
// AtomicInteger底层使用了非阻塞同步的CAS机制

// 该例子的主要目的是引出CAS

public class App {
    static CountDownLatch cd = new CountDownLatch(10);
    AtomicInteger count = new AtomicInteger();

    void f1() {
        for (int i = 1; i <= 1000; i++) {
            count.incrementAndGet();  // ++i
        }
    }

    public static void main(String[] args) throws InterruptedException {
        App app = new App();
        for (int i = 1; i <= 10; i++) {
           new Thread(() -> {
               app.f1();
               cd.countDown();
           }).start();
        }
        cd.await();
        System.out.println(app.count.get());
    }
}